// pages/confirm/confirm.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    display:"none",
    _num: 1,
    order_no:'',
    coupon:'优惠券',
    array: [],
    useid:'-1',
    logo: false,
    distribution_pay: 1, //0-->自提 1-->配送,
  },
  //优惠券
  bindPickerChange: function (e) {
    let that = this;
    var money = that.data.money;    //价格    Total
    var Total = that.data.Total;     //合计价格
    //使用优惠券
    if (that.data.array[e.detail.value].useid != -1) {
      console.log('使用优惠券')
      if (money >= that.data.array[e.detail.value].full_money) {
        Total = money - that.data.array[e.detail.value].money
        that.setData({
          index: e.detail.value,
          coupon: that.data.array[e.detail.value].name,
          useid: that.data.array[e.detail.value].useid,
          Total: Total
        })
      } else {      //优惠券未达到额度
        wx.showToast({
          title: '优惠券未达到使用额度',
          icon: 'none'
        })
        Total = money
        that.setData({
          coupon: '未使用优惠券',
          useid: -1,
          Total: money
        })
      }
    } else {
      that.setData({
        coupon: '未使用优惠券',
        useid: -1,
        Total: money
      })
      console.log('未用优惠券')
    }

    // console.log(Total)
    // console.log('picker发送选择改变，携带值为', e.detail.value)


  },
  liuyan:function(e){
      this.setData({
        liuyan:e.detail.value
      })
  },
  show_payment:function(){
    this.setData({
      display:"block"
    })
    
  },
  hideview: function () {
    let  that=this;
    var type=that.data.type;
    var attrid = that.data.attrid;
    var order_no = '';
    if(type==1){
      wx.showModal({
        title: '提示',
        content: '您确定参加团购吗',
        success:function(e){
          if(e.confirm==1){
            wx.showLoading({
              title: '请稍后',
            })
            wx.request({
              url: app.data.Url +'goods/SpellInsert',
              data:{
                addressid: that.data.add.uaid,
                uid:wx.getStorageSync('userInfo').uid,
                attrid:attrid,
                num:that.data.num,
                remark: that.data.liuyan ? that.data.liuyan:'',
                spell:1,
                spell_id: that.data.spell_id,
                goodsid: that.data.goodsid,
                sid:that.data.sid,
                useid: that.data.useid,
                distribution_pay: that.data.distribution_pay
              },
              method:'post',
              success:function(e){
                console.log(e.data.code,'e.data.code')
                // if (e.data.code == 0) {
                if(e.data.code == 0){
                  wx.showToast({
                    title: '您已经参与此拼团',
                    icon:'icon',
                    duration:1000
                  })
                }else{
                  wx.request({
                    url: app.data.Url + 'order/orderPayParams',
                    data: {
                      order_no: e.data.order_no,
                      title: '智慧社区订单支付',
                      uid: wx.getStorageSync('userInfo').uid
                    },
                    method: 'post',
                    success: function (res) {
                      console.log(res.data.data.weixin)
                      wx.requestPayment({
                        'timeStamp': res.data.data.weixin.timeStamp,
                        'nonceStr': res.data.data.weixin.nonceStr,
                        'package': res.data.data.weixin.package,
                        'signType': 'MD5',
                        'paySign': res.data.data.weixin.paySign,
                        'success': function (res) {
                          //不做任何处理的接口
                          console.log('请求接口')
                          wx.request({
                            url: 'https://zhapp.4hl.cn/app/pay/notify_urls',//app.data.Url + 'app/pay/notify_urls',
                            data: {
                              order_no: e.data.order_no,
                              type:1
                            },
                            method: 'post',
                            success: function (res) {
                              if(res.data.code == 0){
                                wx.navigateTo({
                                  url: '/pages/collage/collage?index=1',
                                })
                              } else if (res.data.code == 1){
                                wx.navigateTo({
                                  url: '/pages/collage/collage?index=3',
                                })
                              }
                            }
                          })
                        },
                        'fail': function (res) {
                          wx.navigateTo({
                            url: '/pages/collage/collage?index=2',
                          })
                        }
                      })
                    }
                  })
                }
                  // if(e.data.code==1){
                  //   wx.showToast({
                  //     title: '团购成功',
                  //     icon: 'none',
                  //     duration:1000,
                  //     success:function(){
                  //       setTimeout(function(){
                  //           wx.navigateTo({
                  //             url: '/pages/collage/collage',
                  //           })
                  //       }, 1000)
                  //     }
                  //   })
                  // }else{
                  //   wx.hideLoading();
                  //   wx.showToast({
                  //     title: e.data.msg,
                  //     icon:'none'
                  //   })
                  // }
              }
            })
            that.setData({
              display: "none"
            })
          }
        }
      })
    }
    
  },
  pay:function(){
    wx.request({
      url: app.data.Url + 'order/orderPayParams',
      data: {
        addressid: that.data.add.uaid,
        order_no: that.data.order_no,
        title: '智慧社区订单支付',
        uid: wx.getStorageSync('userInfo').uid
      },
      method: 'post',
      success: function (res) {
        console.log(res.data.data.weixin)
        wx.requestPayment({
          'timeStamp': res.data.data.weixin.timeStamp,
          'nonceStr': res.data.data.weixin.nonceStr,
          'package': res.data.data.weixin.package,
          'signType': 'MD5',
          'paySign': res.data.data.weixin.paySign,
          'success': function (res) {

          },
          'fail': function (res) {

          }
        })
      }
    })
  },
  focus_click: function (e) {
    console.log(e.target.dataset.num);
    if (e.target.dataset.num==2){
      //自提
      this.setData({
        distribution_pay:0
      })
    }else{
      this.setData({
        distribution_pay: 1
      })
    }
    this.setData({
      _num: e.target.dataset.num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    that.setData({
      ontype: options.ontype ? options.ontype : false,
      spell_id: options.id ? options.id:'',
      goodsid: options.goodsid,
      sid: options.sid
    })
    //判断是否登录
    if (!wx.getStorageSync('userInfo')) {
      that.setData({
        logo: true
      })
    }
    console.log(options.rule,'options.type')
    wx.request({
      url: app.data.Url +'goods/detail',
      data:{
        goodsid:options.goodsid,
        sid: options.sid
      },
      success:function(res){
        console.log(res,'res')
        that.setData({
          pic: res.data.data.pic,
          
        })
        console.log(res)
      if(options.type==1){
        if (!res.data.data.specification[0]) {
          that.setData({
            detail: res.data.data,
            num: options.num,
            money: res.data.data.min_price * options.num,
            Total: res.data.data.min_price * options.num,
            goodsid: options.goodsid,
          })
        }else{
          that.setData({
            detail: res.data.data,
            num: options.num,
            attrid: res.data.data.specification[options.rule].attrid,
            money: res.data.data.specification[options.rule].price * options.num,
            Total: res.data.data.specification[options.rule].price * options.num,
            goodsid: options.goodsid,
          })
        }              
      }else{
        that.setData({
          detail: res.data.data,
          num: options.num,
          attrid: res.data.data.specification[options.rule].attrid,
          money: res.data.data.specification[options.rule].min_price * options.num,
          Total: res.data.data.specification[options.rule].min_price * options.num,
          goodsid: options.goodsid,
          attrid: res.data.data.specification[options.rule].attrid,
          type:1
        })
        
      }
     }
    })
    //https://zhapp.4hl.cn/app/Coupon/usercoupon?uid=XXX&youtype=XXX
    wx.request({
      url: 'https://zhapp.4hl.cn/app/Coupon/usercoupon',
      data:{
        uid:wx.getStorageSync('userInfo').uid,
        youtype:1
      },
      method:'get',
      success:function(res){
        console.log(res.data)
        that.setData({
          array: res.data.data
        })
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    console.log('onShow')
    //获取缓存  判断 选择的地址
    wx.getStorage({
      key: 'address',
      success: function (res) {
        console.log('读取缓存成功')
        console.log(res)
        that.setData({
          add: res.data
        })
        wx.removeStorage({
          key: 'address',
          success: function (res) {
            console.log(res.data)
          }
        })
      },
      fail: function () {
        console.log('读取缓存失败')
        wx.request({
          url: app.data.Url + 'address/get_defaultadd',
          data: {
            uid: wx.getStorageSync('userInfo').uid,
          },
          method: 'get',
          success: function (res) {
            console.log(res,'-----')
            if (!res.data.data.city) {
              wx.showModal({
                title: '提示',
                content: '请添加默认地址',
                showCancel: false,
                success: function () {
                  wx.navigateTo({
                    url: '/pages/address/address',
                  })
                }
              })
            } else {
              that.setData({
                add: res.data.data
              })
            }
          }
        })
      }
    })
  },
  //登录
  sendRequest: function (res) {
    let that = this;
    if (!res.detail.rawData) {
      wx.showModal({
        title: '提示',
        content: '请重新点击授权',
      })
      wx.hideLoding()
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    wx.login({
      success: function (code) {
        var code = code.code;
        wx.request({
          url: app.data.Url + 'User/logins',
          data: {
            utoken: '',
            code: code,
            encryptedData: res.detail.encryptedData,
            iv: res.detail.iv,
            bid: 123
          },
          method: 'post',
          success: function (rs) {
            if (rs.data.success == 10) {
              wx.request({
                url: app.data.Url + 'User/getUid',
                data: {
                  openid: rs.data.openid
                },
                method: 'post',
                success: function (e) {
                  // if (!e.data.data) wx.showModal({
                  //   title: '提示',
                  //   content: '请重新点击授权',
                  // })
                  // wx.hideLoading()
                  wx.setStorageSync('userInfo', {
                    head: res.detail.userInfo.avatarUrl,
                    name: res.detail.userInfo.nickName,
                    sex: res.detail.userInfo.gender,
                    uid: e.data.data,
                    openid: rs.data.openid
                  })
                  wx.request({
                    url: app.data.Url + 'user/get_shop',
                    data: {
                      uid: wx.getStorageSync('userInfo').uid
                    },
                    method: 'get',
                    success: function (res) {
                      if (res.data.data.id == '' || res.data.data.id == undefined || res.data.data.id == null) {
                        wx.getLocation({
                          type: 'wgs84',
                          success: function (res) {
                            var latitude = res.latitude
                            var longitude = res.longitude
                            wx.request({
                              url: app.data.Url + 'wisdom/get_add',
                              data: {
                                latitude: latitude,
                                longitude: longitude
                              },
                              method: 'post',
                              success: function (res) {
                                console.log(res.data.data.bid, 'res.data.data')
                                app.data.address.name = res.data.data.name
                                app.data.address.id = res.data.data.bid
                                that.setData({
                                  address: res.data.data.name,
                                  bid: res.data.data.bid
                                })
                              }
                            })
                          }
                        })
                        console.log('true')
                      } else {
                        //绑定社区
                        app.data.address.name = res.data.data.name
                        app.data.address.id = res.data.data.id
                        console.log(app.data.address, '=============')
                        that.setData({
                          address: res.data.data.name,
                          bid: res.data.data.id
                        })
                      }
                      that.setData({
                        logo: false
                      })
                      wx.hideLoading()
                    }
                  })

                  //请求绑定社区
                  wx.setStorageSync('phone', e.data.phone)

                  // wx.showTabBar({
                  //   success: function () {
                  //     that.setData({
                  //       logo: false
                  //     })
                  //     wx.showTabBar({

                  //     })
                  //     wx.hideLoading()
                  //   }
                  // })

                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '请重新点击授权',
              })
              wx.hideLoading()
              return false;
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.ontype) {
      app.navTo('../collageWare/collageWare?id=' + this.data.sid + '&goodsid=' + this.data.goodsid + '&ontype=0')
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})